define(['./app'], function (app) {
    'use strict';
    return app.run(['$rootScope','$state','$stateParams','$cookieStore','$location',
            function ($rootScope, $state,$stateParams,$cookieStore,$location) {
                $rootScope.$state = $state;
                $rootScope.$stateParams = $stateParams;
                $rootScope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){
                    //进入登录不做处理,删除cookie和 root
                    if(toState.name=='login'){// 如果是进入登录界面则允许
                        $cookieStore.remove("wq_use");
                        $rootScope.user = null;
                        return;
                    }
                    //如果未登录,获取cookie 数据
                    if(!$rootScope.user){
                        if($cookieStore.get("wq_use")!=undefined){
                            $rootScope.user=$cookieStore.get("wq_use");
                            $rootScope.token = $rootScope.user.token;
                        }
                    }
                    //进入用户列表,未登录就去登录
                    if(toState.name.indexOf("home")!=-1&&!$rootScope.user){
                        event.preventDefault();// 取消默认跳转行为
                        $state.go("login");
                    }
                    if(toState.name=="commit-order"&&!$rootScope.user){
                        event.preventDefault();// 取消默认跳转行为
                        $state.go("login",{returl:toState.name+"/"+(toParams["lineid"]!=undefined?toParams["lineid"]:"")});
                    }

                });
            }
        ]
    ).config(['$stateProvider', '$urlRouterProvider',
        function ($stateProvider, $urlRouterProvider) {
            $urlRouterProvider.otherwise("/index");
            $stateProvider
                //首页
                .state('index', {
                    url: "/index",
                    templateUrl: 'partials/index.html'
                })
                //线路详情
                .state('detail', {
                    url: "/detail/:lineid",
                    controller:"detailCtrl",
                    templateUrl: 'partials/detail.html'
                })
                //还款指南
                .state('refund-intro', {
                    url: "/refund-intro",
                    templateUrl: 'partials/refund-intro.html'
                })
                //帮助中心
                .state('user-help', {
                    url: "/user-help",
                    templateUrl: 'partials/user-help.html'
                })
                //联系我们
                .state('contact', {
                    url: "/contact",
                    templateUrl: 'partials/contact.html'
                })
                //提交订单
                .state('commit-order', {
                    url: "/commit-order/:lineid",
                    //params: {
                    //    lineid: null
                    //},
                    controller:'commitOrderCtrl',
                    templateUrl: 'partials/commit-order.html'
                })
                //付款
                .state('pay', {
                    url: "/pay",
                    templateUrl: 'partials/pay.html'
                })
                //用户首页
                .state('home', {
                    url: "/home",
                    controller:'homeCtrl',
                    templateUrl: 'partials/home/home.html'
                })
                // user 会员管理
                .state('home.list', {
                    url: '/list',
                    templateUrl: 'partials/home/list.html'
                })
                // 通知中心
                .state('home.notice', {
                    url: "/notice",
                    templateUrl: 'partials/home/notice.html'
                })
                //我的优惠券
                .state('home.hongbao', {
                    url: "/hongbao",
                    templateUrl: 'partials/home/hongbao.html'
                })
                //我的订单
                .state('home.orderlist', {
                    url: "/orderlist",
                    templateUrl: 'partials/home/orderlist.html'
                })
                //订单详情
                .state('home.orderdetail', {
                    url: "/orderdetail",
                    templateUrl: 'partials/home/orderdetail.html'
                })
                //查看账单
                .state('home.bill', {
                    url: "/bill",
                    controller:"homeBillCtrl",
                    templateUrl: 'partials/home/bill.html'
                })
                //认证资料
                .state('home.edit-user-info', {
                    url: "/edit-user-info",
                    templateUrl: 'partials/home/edit-user-info.html'
                })
                //修改密码
                .state('home.edit-password', {
                    url: "/edit-password",
                    templateUrl: 'partials/home/edit-password.html'
                })
                //设置邮箱
                .state('home.set-mail', {
                    url: "/set-mail",
                    templateUrl: 'partials/home/set-mail.html'
                })
                //注册
                .state('reg_step1', {
                    url: "/reg",
                    controller:"regCtrl",
                    templateUrl: 'partials/reg/reg_step1.html'
                })
                .state('reg_step2', {
                    url: "/reg",
                    templateUrl: 'partials/reg/reg_step2.html'
                })
                //登陆
                .state('login', {
                    url: "/login/:returl",
                    controller:"loginCtrl",
                    templateUrl: 'partials/login.html'
                })
                //找回密码
                .state('find-password', {
                    url: "/find-password",
                    templateUrl: 'partials/find-password.html'
                })
                .state('invitation', {
                    url: "/invitation",
                    templateUrl: 'partials/invitation.html'
                })
               ;

        }]);
});